package com.chuangmei.com.arindoorlocationsystem.video;

import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.WindowManager;
import android.widget.MediaController;
import android.widget.RelativeLayout;

import com.chuangmei.com.arindoorlocationsystem.R;
import com.chuangmei.com.arindoorlocationsystem.base.BaseActivity;
import com.chuangmei.com.arindoorlocationsystem.utils.LogUtils;

import butterknife.BindView;

public class VideoPlayActivity extends BaseActivity {


    @BindView(R.id.mVV_play_Video)
    FullScreenVideoView mVV_play_Video;
    private String video_uri;
    private int video_play_position = 0;


    @Override
    protected void initLayout() {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.actionBar_hide = true;//横屏播放隐藏状态栏
        setContentView(R.layout.activity_video_play);
    }

    @Override
    protected void initView() {
        Bundle bundle = getIntent().getExtras();
        video_uri = bundle.getString("video_uri");
        video_play_position = bundle.getInt("video_play_position");
        int video_screen_orientation = bundle.getInt("video_screen_orientation");
        setRequestedOrientation(video_screen_orientation);
        playVideo();
    }


    public void playVideo() {
        if (TextUtils.isEmpty(video_uri)) {
            LogUtils.showToast("视频加载错误");
            finish();
        }
        //将视频平铺父布局
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.FILL_PARENT,
                RelativeLayout.LayoutParams.FILL_PARENT);
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        mVV_play_Video.setLayoutParams(layoutParams);
        MediaController mctrl = new MediaController(this);
        mVV_play_Video.setMediaController(mctrl);
    }


    @Override
    protected void onResume() {
        super.onResume();
        mVV_play_Video.setVideoURI(Uri.parse(video_uri));
        mVV_play_Video.seekTo(video_play_position);
        if (!mVV_play_Video.isPlaying()) {
            mVV_play_Video.start();
        } else {
            mVV_play_Video.resume();
        }
        mVV_play_Video.requestFocus();
    }

    @Override
    protected void onPause() {
        super.onPause();
        video_play_position = mVV_play_Video.getCurrentPosition();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        mVV_play_Video.resume();
    }
}
